<?php

/**
 * @see Zend_Validate_Abstract
 */
require_once 'Zend/Validate/Abstract.php';


class Core_Validate_IdenticalTo extends Zend_Validate_Abstract
{
	/**
	 * Validation failure message key for when the value is not unique.
	 */
	const NOT_IDENTICAL = 'notIdentical';
	
	/**
	 * Validation failure message template definitions
	 *
	 * @var array
	 */
	protected $_messageTemplates = array(
		self::NOT_IDENTICAL	=> "'%value%' does not match."
	);
	
	private $_fieldName;
	
	public function __construct (  $fieldName )
	{
		$this->_fieldName = $fieldName;
	}

	/**
	 * Defined by Zend_Validate_Interface
	 *
	 * Returns true if and only if $value contains only alphabetic and digit characters
	 *
	 * @param  string $value
	 * @return boolean
	 */
	public function isValid($value, $context = null)
	{
		$value = (string) $value;
		
		$this->_setValue($value);
		
		if (is_array($context))
		{
			if ( isset($context[$this->_fieldName]) && ($value == $context[$this->_fieldName]) )
			{
				return true;
			}
		}
		elseif (is_string($context) && ($value == $context))
		{
			return true;
		}
		
        $this->_error(self::NOT_IDENTICAL);
        
        return false;
	}

}
